﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using XclusiveService.Models;

namespace XclusiveService.Controllers
{
    public class OrderController : ApiController
    {
        static readonly IOrderRepository repository = new OrderRepository();

        // Retrieve Resource
        public IEnumerable<Order> GetAllOrder()
        {
            return repository.GetAll();
        }

        public Order GetOrder(int id)
        {
            Order item = repository.GetById(id);
            if (item == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            return item;
        }

        // Create Resource
        public HttpResponseMessage PostOrder(Order item)
        {
            item = repository.Add(item);
            var response = Request.CreateResponse<Order>(HttpStatusCode.Created, item);

            string uri = Url.Link("DefaultApi", new { id = item.orderID });
            response.Headers.Location = new Uri(uri);
            return response;
        }

        // Update Resource
        public void PutOrder(int id, Order Order)
        {
            Order.orderID = id;
            if (!repository.Update(Order))
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
        }

        // Delete Resource
        public HttpResponseMessage DeleteOrder(int id)
        {
            repository.Remove(id);
            return new HttpResponseMessage(HttpStatusCode.NoContent);
        }
    }
}
